<?php
namespace ClubAdmin\Controller;
use Think\Controller;
class CourseController extends Controller {
     public function courselist(){

        $coursesModel=M('courses');//构建一个操作masters表的模型实例
        $courses=$coursesModel->order('c_id ')->select();
        $this->courselist=$courses;
        // dump($masters);
        $this->display();
       

    }
    public function add(){
        $this->display();
    }
    public function addCourse(){
       if($_FILES['file']['error']==0 ){
            $info=$this->upload();
            $name=$info["file"]["savename"];
        }
        else{
            $name="default.jpg";
        }
        $course = D('courses');


        if($course->create()){
            $course->c_pic=$name; 
            if(false!==$course->add())
                      {
                $this->success('发布成功!','courselist');
            }
            else{$this->error('发布失败');}
        }else{
            $this->error($course->getError());
        }
    }
    public function upload(){
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =     './Public/Upload/'; // 设置附件上传根目录  //  
        $upload->autoSub = false;//关闭子目录，默认true
        //$upload->savePath  =     ''; // 设置附件上传(子)目录，默认目录名为年月日
        $info   =   $upload->upload();  // 上传文件 
        if(!$info) {
            $this->error($upload->getError());    
        }
        else{
            return $info;
        }                    
    }
     public function delcourse($c_id){
    $coursesModel=M('courses'); 
    if (false!==$coursesModel->where("c_id=$c_id"/*必须是双引号*/)->delete()) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败');
        }
}

    public  function editCourse($c_id){
        $coursesModel = M('courses');
        $course=$coursesModel->where("c_id=$c_id")->find();
        $this->coursesInfo=$course;
        $this->display();
    }

    public function  doUpdate($c_id){
        $coursesInfo = M('courses');
        // $data['m_intro']=$_SESSION['m_intro'];
        // $i = $data['m_intro'];
        //接收用户输入的内容
        $data['c_intro'] = trim(I('post.c_intro'));
        $data['c_name'] = trim(I('post.c_name'));
        $data['c_address'] = trim(I('post.c_address'));

        if ($_FILES['file']['error'] == 0) {

                $info = $this->upload();
                $data['c_pic'] = $info['file']['savename'];
            } else {
                $data['photo'] = "default.jpg";
            }


        $course=$coursesInfo->where("c_id=$c_id")->save($data);

        if(false !== $course){
            $this->success('修改成功！','courselist');
        }else{
            $this->error('修改失败');
        }
    }
}